/*
 * userinfo_dao.c
 *
 *  Created on: 2013-2-6
 *      Author: panqiuhui
 */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>

#include "../head/dao/userinfo_dao.h"

extern MYSQL_STMT *get_userinfo_stmt;
extern MYSQL_STMT *update_userinfo_stmt;
extern MYSQL_STMT *change_password_stmt;
extern MYSQL_STMT *register_new_user_stmt;

enum {
	TABLE_CUSTOMERS_SELECT_USERNAME = 0,
	TABLE_CUSTOMERS_SELECT_PASSWORD,
	TABLE_CUSTOMERS_SELECT_NAME,
	TABLE_CUSTOMERS_SELECT_GENDER,
	TABLE_CUSTOMERS_SELECT_BIRTHDAY,
	TABLE_CUSTOMERS_SELECT_COUNTRY,
	TABLE_CUSTOMERS_SELECT_CREDIT_TYPE,
	TABLE_CUSTOMERS_SELECT_CREDIT_ID,
	TABLE_CUSTOMERS_SELECT_MOBILE_PHONE_NUM,
	TABLE_CUSTOMERS_SELECT_FIXED_PHONE_NUM,
	TABLE_CUSTOMERS_SELECT_EMAIL,
	TABLE_CUSTOMERS_SELECT_ADDRESS,
	TABLE_CUSTOMERS_SELECT_POSTCODE,
	TABLE_CUSTOMERS_SELECT_TYPE,
	TABLE_CUSTOMERS_SELECT_COLUMN_NUM
};

enum {
	TABLE_CUSTOMERS_UPDATE_NAME = 0,
	TABLE_CUSTOMERS_UPDATE_GENDER,
	TABLE_CUSTOMERS_UPDATE_BIRTHDAY,
	TABLE_CUSTOMERS_UPDATE_COUNTRY,
	TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE,
	TABLE_CUSTOMERS_UPDATE_CREDIT_ID,
	TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM,
	TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM,
	TABLE_CUSTOMERS_UPDATE_EMAIL,
	TABLE_CUSTOMERS_UPDATE_ADDRESS,
	TABLE_CUSTOMERS_UPDATE_POSTCODE,
	TABLE_CUSTOMERS_UPDATE_USERNAME,
	TABLE_CUSTOMERS_UPDATE_COLUMN_NUM
};

enum {
	TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD = 0,
	TABLE_CUSTOMERS_CHANGEPW_USERNAME,
	TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD,
	TABLE_CUSTOMERS_CHANGEPW_COLUMN_NUM
};

enum {
	TABLE_CUSTOMERS_INSERT_USERNAME = 0,
	TABLE_CUSTOMERS_INSERT_PASSWORD,
	TABLE_CUSTOMERS_INSERT_NAME,
	TABLE_CUSTOMERS_INSERT_GENDER,
	TABLE_CUSTOMERS_INSERT_BIRTHDAY,
	TABLE_CUSTOMERS_INSERT_COUNTRY,
	TABLE_CUSTOMERS_INSERT_CREDIT_TYPE,
	TABLE_CUSTOMERS_INSERT_CREDIT_ID,
	TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM,
	TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM,
	TABLE_CUSTOMERS_INSERT_EMAIL,
	TABLE_CUSTOMERS_INSERT_ADDRESS,
	TABLE_CUSTOMERS_INSERT_POSTCODE,
	TABLE_CUSTOMERS_INSERT_TYPE,
	TABLE_CUSTOMERS_INSERT_COLUMN_NUM
};

//得到用户的信息，成功则返回true并设置用户信息，失败则返回false，并将用户信息设置为null
bool get_userinfo(const char *username, const char *password,
		struct userinfo *user) {
	bool result = true;
	//设置输入的参数信息
	MYSQL_BIND input_bind[2];
	char input_username[USERNAME_LEN];
	unsigned long input_username_len;
	char input_password[PASSWORD_LEN];
	unsigned long input_password_len;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer = input_username;
	input_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer_length = USERNAME_LEN;
	input_bind[TABLE_CUSTOMERS_SELECT_USERNAME].is_null = 0;
	input_bind[TABLE_CUSTOMERS_SELECT_USERNAME].length = &input_username_len;

	input_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer = input_password;
	input_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer_length = PASSWORD_LEN;
	input_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].is_null = 0;
	input_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].length = &input_password_len;

	//一定要记得初始化
	memset(input_username, 0, USERNAME_LEN);
	memset(input_password, 0, PASSWORD_LEN);
	strncpy(input_username, username, strlen(username));
	strncpy(input_password, password, strlen(password));
	input_username_len = strlen(input_username);
	input_password_len = strlen(input_password);

	//设置输出的参数信息
	MYSQL_BIND output_bind[TABLE_CUSTOMERS_SELECT_COLUMN_NUM];
	MYSQL_TIME output_birthday;
	unsigned long output_length[TABLE_CUSTOMERS_SELECT_COLUMN_NUM];
	char output_username[USERNAME_LEN];
	char output_password[PASSWORD_LEN];
	char output_name[MY_NAME_LEN];
	char output_gender[GENDER_LEN];
	char output_country[COUNTRY_LEN];
	char output_creditType[CREDIT_TYPE_LEN];
	char output_creditID[CREDIT_ID_LEN];
	char output_mobilephoneNum[MOBILE_PHONE_NUM_LEN];
	char output_fixedphoneNum[FIXED_PHONE_NUM_LEN];
	char output_email[EMAIL_LEN];
	char output_address[ADDRESS_LEN];
	char output_postcode[POSTCODE_LEN];
	char output_type[TYPE_LEN];
	my_bool output_is_null[TABLE_CUSTOMERS_SELECT_COLUMN_NUM];
	my_bool output_error[TABLE_CUSTOMERS_SELECT_COLUMN_NUM];

	memset(output_bind, 0, sizeof(output_bind));

	output_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer = output_username;
	output_bind[TABLE_CUSTOMERS_SELECT_USERNAME].buffer_length = USERNAME_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer = output_password;
	output_bind[TABLE_CUSTOMERS_SELECT_PASSWORD].buffer_length = PASSWORD_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_NAME].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_NAME].buffer = output_name;
	output_bind[TABLE_CUSTOMERS_SELECT_NAME].buffer_length = MY_NAME_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_GENDER].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_GENDER].buffer = output_gender;
	output_bind[TABLE_CUSTOMERS_SELECT_GENDER].buffer_length = GENDER_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_BIRTHDAY].buffer_type = MYSQL_TYPE_DATE;
	output_bind[TABLE_CUSTOMERS_SELECT_BIRTHDAY].buffer = &output_birthday;

	output_bind[TABLE_CUSTOMERS_SELECT_COUNTRY].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_COUNTRY].buffer = output_country;
	output_bind[TABLE_CUSTOMERS_SELECT_COUNTRY].buffer_length = COUNTRY_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_TYPE].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_TYPE].buffer = output_creditType;
	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_TYPE].buffer_length =
			CREDIT_TYPE_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_ID].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_ID].buffer = output_creditID;
	output_bind[TABLE_CUSTOMERS_SELECT_CREDIT_ID].buffer_length = CREDIT_ID_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_MOBILE_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_MOBILE_PHONE_NUM].buffer =
			output_mobilephoneNum;
	output_bind[TABLE_CUSTOMERS_SELECT_MOBILE_PHONE_NUM].buffer_length =
			MOBILE_PHONE_NUM_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_FIXED_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_FIXED_PHONE_NUM].buffer =
			output_fixedphoneNum;
	output_bind[TABLE_CUSTOMERS_SELECT_FIXED_PHONE_NUM].buffer_length =
			FIXED_PHONE_NUM_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_EMAIL].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_EMAIL].buffer = output_email;
	output_bind[TABLE_CUSTOMERS_SELECT_EMAIL].buffer_length = EMAIL_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_ADDRESS].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_ADDRESS].buffer = output_address;
	output_bind[TABLE_CUSTOMERS_SELECT_ADDRESS].buffer_length = ADDRESS_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_POSTCODE].buffer_type =
			MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_POSTCODE].buffer = output_postcode;
	output_bind[TABLE_CUSTOMERS_SELECT_POSTCODE].buffer_length = POSTCODE_LEN;

	output_bind[TABLE_CUSTOMERS_SELECT_TYPE].buffer_type = MYSQL_TYPE_STRING;
	output_bind[TABLE_CUSTOMERS_SELECT_TYPE].buffer = output_type;
	output_bind[TABLE_CUSTOMERS_SELECT_TYPE].buffer_length = TYPE_LEN;

	int i = 0;
	for (i = 0; i < TABLE_CUSTOMERS_SELECT_COLUMN_NUM; i++) {
		output_bind[i].is_null = &output_is_null[i];
		output_bind[i].length = &output_length[i];
		output_bind[i].error = &output_error[i];
	}

	if (mysql_stmt_bind_param(get_userinfo_stmt, input_bind)) {
		fprintf(stderr, "get_userinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(get_userinfo_stmt)) {
		fprintf(stderr, "get_userinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_bind_result(get_userinfo_stmt, output_bind)) {
		fprintf(stderr, "get_userinfo_stmt mysql_stmt_bind_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_store_result(get_userinfo_stmt)) {
		fprintf(stderr, "get_userinfo_stmt mysql_stmt_store_result() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(get_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int rownum = mysql_stmt_num_rows(get_userinfo_stmt);
	//printf("rownums: %d\n", rownum);

	if (rownum > 0) {
		while (!mysql_stmt_fetch(get_userinfo_stmt)) {
			if (!output_is_null[TABLE_CUSTOMERS_SELECT_USERNAME]) {
				sprintf(user->username, "%s", output_username);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_PASSWORD]) {
				sprintf(user->password, "%s", output_password);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_NAME]) {
				sprintf(user->name, "%s", output_name);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_GENDER]) {
				sprintf(user->gender, "%s", output_gender);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_BIRTHDAY]) {
				sprintf(user->birthday, "%04d-%02d-%02d", output_birthday.year,
						output_birthday.month, output_birthday.day);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_COUNTRY]) {
				sprintf(user->country, "%s", output_country);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_CREDIT_TYPE]) {
				sprintf(user->creditType, "%s", output_creditType);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_CREDIT_ID]) {
				sprintf(user->creditID, "%s", output_creditID);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_MOBILE_PHONE_NUM]) {
				sprintf(user->mobilephoneNum, "%s", output_mobilephoneNum);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_FIXED_PHONE_NUM]) {
				sprintf(user->fixedphoneNum, "%s", output_fixedphoneNum);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_EMAIL]) {
				sprintf(user->email, "%s", output_email);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_ADDRESS]) {
				sprintf(user->address, "%s", output_address);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_POSTCODE]) {
				sprintf(user->postcode, "%s", output_postcode);
			}

			if (!output_is_null[TABLE_CUSTOMERS_SELECT_TYPE]) {
				sprintf(user->type, "%s", output_type);
			}
			break;
		}
		result = true;
	} else {
		result = false;
	}
	return result;
}

//判断用户名和密码是否匹配，身份验证通过则返回true并设置用户信息，失败则返回false，并将用户信息设置为null
bool islogin(const struct login_request *request, struct userinfo *user) {
	memset(user, 0, sizeof(*user));
	bool is_have = get_userinfo(request->username, request->password, user);
	return is_have;
}

//更新用户信息，成功返回true，失败返回false
bool update_userinfo(const struct userinfo_request *request) {
	bool result = true;
	//设置输入的参数信息
	MYSQL_BIND input_bind[TABLE_CUSTOMERS_UPDATE_COLUMN_NUM];
	char input_username[USERNAME_LEN];
	unsigned long input_username_len;
	char input_name[NAME_LEN];
	unsigned long input_name_len;
	char input_gender[GENDER_LEN];
	unsigned long input_gender_len;
	char input_birthday[BIRTHDAY_LEN];
	unsigned long input_birthday_len;
	char input_country[COUNTRY_LEN];
	unsigned long input_country_len;
	char input_creditType[CREDIT_TYPE_LEN];
	unsigned long input_credit_type_len;
	char input_creditID[CREDIT_ID_LEN];
	unsigned long input_creditID_len;
	char input_mobilephoneNum[MOBILE_PHONE_NUM_LEN];
	unsigned long input_mobilephoneNum_len;
	char input_fixedphoneNum[FIXED_PHONE_NUM_LEN];
	unsigned long input_fixedphoneNum_len;
	char input_email[EMAIL_LEN];
	unsigned long input_email_len;
	char input_address[ADDRESS_LEN];
	unsigned long input_address_len;
	char input_postcode[POSTCODE_LEN];
	unsigned long input_postcode_len;
	my_bool isnull[TABLE_CUSTOMERS_UPDATE_COLUMN_NUM];

	int i = 0;
	for (i = 0; i < TABLE_CUSTOMERS_UPDATE_COLUMN_NUM; i++) {
		isnull[i] = false;
	}

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_CUSTOMERS_UPDATE_NAME].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_NAME].buffer = input_name;
	input_bind[TABLE_CUSTOMERS_UPDATE_NAME].buffer_length = NAME_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_NAME].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_NAME];
	input_bind[TABLE_CUSTOMERS_UPDATE_NAME].length = &input_name_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_GENDER].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_GENDER].buffer = input_gender;
	input_bind[TABLE_CUSTOMERS_UPDATE_GENDER].buffer_length = GENDER_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_GENDER].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_GENDER];
	input_bind[TABLE_CUSTOMERS_UPDATE_GENDER].length = &input_gender_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_BIRTHDAY].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_BIRTHDAY].buffer = input_birthday;
	input_bind[TABLE_CUSTOMERS_UPDATE_BIRTHDAY].buffer_length = BIRTHDAY_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_BIRTHDAY].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_BIRTHDAY];
	input_bind[TABLE_CUSTOMERS_UPDATE_BIRTHDAY].length = &input_birthday_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_COUNTRY].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_COUNTRY].buffer = input_country;
	input_bind[TABLE_CUSTOMERS_UPDATE_COUNTRY].buffer_length = COUNTRY_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_COUNTRY].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_COUNTRY];
	input_bind[TABLE_CUSTOMERS_UPDATE_COUNTRY].length = &input_country_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE].buffer = input_creditType;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE].buffer_length =
			CREDIT_TYPE_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE];
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE].length =
			&input_credit_type_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_ID].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_ID].buffer = input_creditID;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_ID].buffer_length = CREDIT_ID_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_ID].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_CREDIT_ID];
	input_bind[TABLE_CUSTOMERS_UPDATE_CREDIT_ID].length = &input_creditID_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM].buffer =
			input_mobilephoneNum;
	input_bind[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM].buffer_length =
			MOBILE_PHONE_NUM_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM];
	input_bind[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM].length =
			&input_mobilephoneNum_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM].buffer =
			input_fixedphoneNum;
	input_bind[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM].buffer_length =
			FIXED_PHONE_NUM_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM];
	input_bind[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM].length =
			&input_fixedphoneNum_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_EMAIL].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_EMAIL].buffer = input_email;
	input_bind[TABLE_CUSTOMERS_UPDATE_EMAIL].buffer_length = EMAIL_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_EMAIL].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_EMAIL];
	input_bind[TABLE_CUSTOMERS_UPDATE_EMAIL].length = &input_email_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_ADDRESS].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_ADDRESS].buffer = input_address;
	input_bind[TABLE_CUSTOMERS_UPDATE_ADDRESS].buffer_length = ADDRESS_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_ADDRESS].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_ADDRESS];
	input_bind[TABLE_CUSTOMERS_UPDATE_ADDRESS].length = &input_address_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_POSTCODE].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_POSTCODE].buffer = input_postcode;
	input_bind[TABLE_CUSTOMERS_UPDATE_POSTCODE].buffer_length = POSTCODE_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_POSTCODE].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_POSTCODE];
	input_bind[TABLE_CUSTOMERS_UPDATE_POSTCODE].length = &input_postcode_len;

	input_bind[TABLE_CUSTOMERS_UPDATE_USERNAME].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_UPDATE_USERNAME].buffer = input_username;
	input_bind[TABLE_CUSTOMERS_UPDATE_USERNAME].buffer_length = USERNAME_LEN;
	input_bind[TABLE_CUSTOMERS_UPDATE_USERNAME].is_null =
			&isnull[TABLE_CUSTOMERS_UPDATE_USERNAME];
	input_bind[TABLE_CUSTOMERS_UPDATE_USERNAME].length = &input_username_len;

	memset(input_name, 0, sizeof(input_name));
	strncpy(input_name, request->user.name, strlen(request->user.name));
	input_name_len = strlen(input_name);
	if (input_name_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_NAME] = true;
	}

	memset(input_gender, 0, sizeof(input_gender));
	strncpy(input_gender, request->user.gender, strlen(request->user.gender));
	input_gender_len = strlen(input_gender);
	if (input_gender_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_GENDER] = true;
	}

	memset(input_birthday, 0, sizeof(input_birthday));
	strncpy(input_birthday, request->user.birthday,
			strlen(request->user.birthday));
	input_birthday_len = strlen(input_birthday);
	if (input_birthday_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_BIRTHDAY] = true;
	}

	memset(input_country, 0, sizeof(input_country));
	strncpy(input_country, request->user.country,
			strlen(request->user.country));
	input_country_len = strlen(input_country);
	if (input_country_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_COUNTRY] = true;
	}

	memset(input_creditType, 0, sizeof(input_creditType));
	strncpy(input_creditType, request->user.creditType,
			strlen(request->user.creditType));
	input_credit_type_len = strlen(input_creditType);
	if (input_credit_type_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_CREDIT_TYPE] = true;
	}

	memset(input_creditID, 0, sizeof(input_creditID));
	strncpy(input_creditID, request->user.creditID,
			strlen(request->user.creditID));
	input_creditID_len = strlen(input_creditID);
	if (input_creditID_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_CREDIT_ID] = true;
	}

	memset(input_mobilephoneNum, 0, sizeof(input_mobilephoneNum));
	strncpy(input_mobilephoneNum, request->user.mobilephoneNum,
			strlen(request->user.mobilephoneNum));
	input_mobilephoneNum_len = strlen(input_mobilephoneNum);
	if (input_mobilephoneNum_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_MOBILE_PHONE_NUM] = true;
	}

	memset(input_fixedphoneNum, 0, sizeof(input_fixedphoneNum));
	strncpy(input_fixedphoneNum, request->user.fixedphoneNum,
			strlen(request->user.fixedphoneNum));
	input_fixedphoneNum_len = strlen(input_fixedphoneNum);
	if (input_fixedphoneNum_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_FIXED_PHONE_NUM] = true;
	}

	memset(input_email, 0, sizeof(input_email));
	strncpy(input_email, request->user.email, strlen(request->user.email));
	input_email_len = strlen(input_email);
	if (input_email_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_EMAIL] = true;
	}

	memset(input_address, 0, sizeof(input_address));
	strncpy(input_address, request->user.address,
			strlen(request->user.address));
	input_address_len = strlen(input_address);
	if (input_address_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_ADDRESS] = true;
	}

	memset(input_postcode, 0, sizeof(input_postcode));
	strncpy(input_postcode, request->user.postcode,
			strlen(request->user.postcode));
	input_postcode_len = strlen(input_postcode);
	if (input_postcode_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_POSTCODE] = true;
	}

	memset(input_username, 0, sizeof(input_username));
	strncpy(input_username, request->user.username,
			strlen(request->user.username));
	input_username_len = strlen(input_username);
	if (input_username_len == 0) {
		isnull[TABLE_CUSTOMERS_UPDATE_USERNAME] = true;
	}

	if (mysql_stmt_bind_param(update_userinfo_stmt, input_bind)) {
		fprintf(stderr,
				"update_userinfo_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(update_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(update_userinfo_stmt)) {
		fprintf(stderr, "update_userinfo_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(update_userinfo_stmt));
		exit(EXIT_FAILURE);
	}

	int affected_rows = mysql_stmt_affected_rows(update_userinfo_stmt);
	/*fprintf(stdout, "update_userinfo_stmt total affected rows: %lu\n",
	 (unsigned long) affected_rows);*/

	if (affected_rows != 1) /* validate affected rows */
	{
		fprintf(stderr,
				"update_userinfo_stmt invalid affected rows by MySQL\n");
		result = false;
	}

	return result;
}

//更改用户密码，成功返回true，失败返回false
bool change_password(const struct change_pw_request *request) {
	bool result = true;
	//设置输入的参数信息
	MYSQL_BIND input_bind[TABLE_CUSTOMERS_CHANGEPW_COLUMN_NUM];
	char input_newpw[PASSWORD_LEN];
	unsigned long input_newpw_len;
	char input_username[USERNAME_LEN];
	unsigned long input_username_len;
	char input_originalpw[PASSWORD_LEN];
	unsigned long input_originalpw_len;

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD].buffer = input_newpw;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD].buffer_length =
			PASSWORD_LEN;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD].is_null = 0;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_NEW_PASSWORD].length = &input_newpw_len;

	input_bind[TABLE_CUSTOMERS_CHANGEPW_USERNAME].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_USERNAME].buffer = input_username;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_USERNAME].buffer_length = USERNAME_LEN;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_USERNAME].is_null = 0;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_USERNAME].length = &input_username_len;

	input_bind[TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD].buffer =
			input_originalpw;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD].buffer_length =
			PASSWORD_LEN;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD].is_null = 0;
	input_bind[TABLE_CUSTOMERS_CHANGEPW_ORIGINAL_PASSWORD].length =
			&input_originalpw_len;

	memset(input_newpw, 0, sizeof(input_newpw));
	strncpy(input_newpw, request->new_pw, strlen(request->new_pw));
	input_newpw_len = strlen(input_newpw);

	memset(input_username, 0, sizeof(input_username));
	strncpy(input_username, request->username, strlen(request->username));
	input_username_len = strlen(input_username);

	memset(input_originalpw, 0, sizeof(input_originalpw));
	strncpy(input_originalpw, request->original_pw,
			strlen(request->original_pw));
	input_originalpw_len = strlen(input_originalpw);

	if (mysql_stmt_bind_param(change_password_stmt, input_bind)) {
		fprintf(stderr,
				"change_password_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(change_password_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(change_password_stmt)) {
		fprintf(stderr, "change_password_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(change_password_stmt));
		exit(EXIT_FAILURE);
	}

	int affected_rows = mysql_stmt_affected_rows(change_password_stmt);
	/*fprintf(stdout, "change_password_stmt total affected rows: %lu\n",
	 (unsigned long) affected_rows);*/

	if (affected_rows != 1) /* validate affected rows */
	{
		result = false;
	}

	return result;
}

//注册新用户，成功返回true，失败返回false
bool register_new_user(const struct userinfo_request *request) {
	bool result = true;
	//设置输入的参数信息
	MYSQL_BIND input_bind[TABLE_CUSTOMERS_INSERT_COLUMN_NUM];
	char input_username[USERNAME_LEN];
	unsigned long input_username_len;
	char input_password[PASSWORD_LEN];
	unsigned long input_password_len;
	char input_name[NAME_LEN];
	unsigned long input_name_len;
	char input_gender[GENDER_LEN];
	unsigned long input_gender_len;
	char input_birthday[BIRTHDAY_LEN];
	unsigned long input_birthday_len;
	char input_country[COUNTRY_LEN];
	unsigned long input_country_len;
	char input_creditType[CREDIT_TYPE_LEN];
	unsigned long input_credit_type_len;
	char input_creditID[CREDIT_ID_LEN];
	unsigned long input_creditID_len;
	char input_mobilephoneNum[MOBILE_PHONE_NUM_LEN];
	unsigned long input_mobilephoneNum_len;
	char input_fixedphoneNum[FIXED_PHONE_NUM_LEN];
	unsigned long input_fixedphoneNum_len;
	char input_email[EMAIL_LEN];
	unsigned long input_email_len;
	char input_address[ADDRESS_LEN];
	unsigned long input_address_len;
	char input_postcode[POSTCODE_LEN];
	unsigned long input_postcode_len;
	char input_type[TYPE_LEN];
	unsigned long input_type_len;
	my_bool isnull[TABLE_CUSTOMERS_INSERT_COLUMN_NUM];

	int i = 0;
	for (i = 0; i < TABLE_CUSTOMERS_INSERT_COLUMN_NUM; i++) {
		isnull[i] = false;
	}

	memset(input_bind, 0, sizeof(input_bind));
	input_bind[TABLE_CUSTOMERS_INSERT_USERNAME].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_USERNAME].buffer = input_username;
	input_bind[TABLE_CUSTOMERS_INSERT_USERNAME].buffer_length = USERNAME_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_USERNAME].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_USERNAME];
	input_bind[TABLE_CUSTOMERS_INSERT_USERNAME].length = &input_username_len;

	input_bind[TABLE_CUSTOMERS_INSERT_PASSWORD].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_PASSWORD].buffer = input_password;
	input_bind[TABLE_CUSTOMERS_INSERT_PASSWORD].buffer_length = PASSWORD_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_PASSWORD].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_PASSWORD];
	input_bind[TABLE_CUSTOMERS_INSERT_PASSWORD].length = &input_password_len;

	input_bind[TABLE_CUSTOMERS_INSERT_NAME].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_NAME].buffer = input_name;
	input_bind[TABLE_CUSTOMERS_INSERT_NAME].buffer_length = NAME_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_NAME].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_NAME];
	input_bind[TABLE_CUSTOMERS_INSERT_NAME].length = &input_name_len;

	input_bind[TABLE_CUSTOMERS_INSERT_GENDER].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_GENDER].buffer = input_gender;
	input_bind[TABLE_CUSTOMERS_INSERT_GENDER].buffer_length = GENDER_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_GENDER].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_GENDER];
	input_bind[TABLE_CUSTOMERS_INSERT_GENDER].length = &input_gender_len;

	input_bind[TABLE_CUSTOMERS_INSERT_BIRTHDAY].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_BIRTHDAY].buffer = input_birthday;
	input_bind[TABLE_CUSTOMERS_INSERT_BIRTHDAY].buffer_length = BIRTHDAY_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_BIRTHDAY].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_BIRTHDAY];
	input_bind[TABLE_CUSTOMERS_INSERT_BIRTHDAY].length = &input_birthday_len;

	input_bind[TABLE_CUSTOMERS_INSERT_COUNTRY].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_COUNTRY].buffer = input_country;
	input_bind[TABLE_CUSTOMERS_INSERT_COUNTRY].buffer_length = COUNTRY_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_COUNTRY].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_COUNTRY];
	input_bind[TABLE_CUSTOMERS_INSERT_COUNTRY].length = &input_country_len;

	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE].buffer = input_creditType;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE].buffer_length =
			CREDIT_TYPE_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE];
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE].length =
			&input_credit_type_len;

	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_ID].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_ID].buffer = input_creditID;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_ID].buffer_length = CREDIT_ID_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_ID].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_CREDIT_ID];
	input_bind[TABLE_CUSTOMERS_INSERT_CREDIT_ID].length = &input_creditID_len;

	input_bind[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM].buffer =
			input_mobilephoneNum;
	input_bind[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM].buffer_length =
			MOBILE_PHONE_NUM_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM];
	input_bind[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM].length =
			&input_mobilephoneNum_len;

	input_bind[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM].buffer_type =
			MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM].buffer =
			input_fixedphoneNum;
	input_bind[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM].buffer_length =
			FIXED_PHONE_NUM_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM];
	input_bind[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM].length =
			&input_fixedphoneNum_len;

	input_bind[TABLE_CUSTOMERS_INSERT_EMAIL].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_EMAIL].buffer = input_email;
	input_bind[TABLE_CUSTOMERS_INSERT_EMAIL].buffer_length = EMAIL_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_EMAIL].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_EMAIL];
	input_bind[TABLE_CUSTOMERS_INSERT_EMAIL].length = &input_email_len;

	input_bind[TABLE_CUSTOMERS_INSERT_ADDRESS].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_ADDRESS].buffer = input_address;
	input_bind[TABLE_CUSTOMERS_INSERT_ADDRESS].buffer_length = ADDRESS_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_ADDRESS].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_ADDRESS];
	input_bind[TABLE_CUSTOMERS_INSERT_ADDRESS].length = &input_address_len;

	input_bind[TABLE_CUSTOMERS_INSERT_POSTCODE].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_POSTCODE].buffer = input_postcode;
	input_bind[TABLE_CUSTOMERS_INSERT_POSTCODE].buffer_length = POSTCODE_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_POSTCODE].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_POSTCODE];
	input_bind[TABLE_CUSTOMERS_INSERT_POSTCODE].length = &input_postcode_len;

	input_bind[TABLE_CUSTOMERS_INSERT_TYPE].buffer_type = MYSQL_TYPE_STRING;
	input_bind[TABLE_CUSTOMERS_INSERT_TYPE].buffer = input_type;
	input_bind[TABLE_CUSTOMERS_INSERT_TYPE].buffer_length = TYPE_LEN;
	input_bind[TABLE_CUSTOMERS_INSERT_TYPE].is_null =
			&isnull[TABLE_CUSTOMERS_INSERT_TYPE];
	input_bind[TABLE_CUSTOMERS_INSERT_TYPE].length = &input_type_len;

	memset(input_name, 0, sizeof(input_name));
	memset(input_username, 0, sizeof(input_username));
	strncpy(input_username, request->user.username,
			strlen(request->user.username));
	input_username_len = strlen(input_username);
	if (input_username_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_USERNAME] = true;
	}

	memset(input_password, 0, sizeof(input_password));
	strncpy(input_password, request->user.password,
			strlen(request->user.password));
	input_password_len = strlen(input_password);
	if (input_password_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_PASSWORD] = true;
	}

	strncpy(input_name, request->user.name, strlen(request->user.name));
	input_name_len = strlen(input_name);
	if (input_name_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_NAME] = true;
	}

	memset(input_gender, 0, sizeof(input_gender));
	strncpy(input_gender, request->user.gender, strlen(request->user.gender));
	input_gender_len = strlen(input_gender);
	if (input_gender_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_GENDER] = true;
	}

	memset(input_birthday, 0, sizeof(input_birthday));
	strncpy(input_birthday, request->user.birthday,
			strlen(request->user.birthday));
	input_birthday_len = strlen(input_birthday);
	if (input_birthday_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_BIRTHDAY] = true;
	}

	memset(input_country, 0, sizeof(input_country));
	strncpy(input_country, request->user.country,
			strlen(request->user.country));
	input_country_len = strlen(input_country);
	if (input_country_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_COUNTRY] = true;
	}

	memset(input_creditType, 0, sizeof(input_creditType));
	strncpy(input_creditType, request->user.creditType,
			strlen(request->user.creditType));
	input_credit_type_len = strlen(input_creditType);
	if (input_credit_type_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_CREDIT_TYPE] = true;
	}

	memset(input_creditID, 0, sizeof(input_creditID));
	strncpy(input_creditID, request->user.creditID,
			strlen(request->user.creditID));
	input_creditID_len = strlen(input_creditID);
	if (input_creditID_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_CREDIT_ID] = true;
	}

	memset(input_mobilephoneNum, 0, sizeof(input_mobilephoneNum));
	strncpy(input_mobilephoneNum, request->user.mobilephoneNum,
			strlen(request->user.mobilephoneNum));
	input_mobilephoneNum_len = strlen(input_mobilephoneNum);
	if (input_mobilephoneNum_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_MOBILE_PHONE_NUM] = true;
	}

	memset(input_fixedphoneNum, 0, sizeof(input_fixedphoneNum));
	strncpy(input_fixedphoneNum, request->user.fixedphoneNum,
			strlen(request->user.fixedphoneNum));
	input_fixedphoneNum_len = strlen(input_fixedphoneNum);
	if (input_fixedphoneNum_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_FIXED_PHONE_NUM] = true;
	}

	memset(input_email, 0, sizeof(input_email));
	strncpy(input_email, request->user.email, strlen(request->user.email));
	input_email_len = strlen(input_email);
	if (input_email_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_EMAIL] = true;
	}

	memset(input_address, 0, sizeof(input_address));
	strncpy(input_address, request->user.address,
			strlen(request->user.address));
	input_address_len = strlen(input_address);
	if (input_address_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_ADDRESS] = true;
	}

	memset(input_postcode, 0, sizeof(input_postcode));
	strncpy(input_postcode, request->user.postcode,
			strlen(request->user.postcode));
	input_postcode_len = strlen(input_postcode);
	if (input_postcode_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_POSTCODE] = true;
	}

	memset(input_type, 0, sizeof(input_type));
	strncpy(input_type, request->user.type, strlen(request->user.type));
	input_type_len = strlen(input_type);
	if (input_type_len == 0) {
		isnull[TABLE_CUSTOMERS_INSERT_TYPE] = true;
	}

	if (mysql_stmt_bind_param(register_new_user_stmt, input_bind)) {
		fprintf(stderr,
				"register_new_user_stmt mysql_stmt_bind_param() failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(register_new_user_stmt));
		exit(EXIT_FAILURE);
	}

	if (mysql_stmt_execute(register_new_user_stmt)) {
		fprintf(stderr,
				"register_new_user_stmt mysql_stmt_execute(), failed\n");
		fprintf(stderr, "%s\n", mysql_stmt_error(register_new_user_stmt));
		exit(EXIT_FAILURE);
	}

	int affected_rows = mysql_stmt_affected_rows(register_new_user_stmt);
	/*fprintf(stdout, "update_userinfo_stmt total affected rows: %lu\n",
	 (unsigned long) affected_rows);*/

	if (affected_rows != 1) /* validate affected rows */
	{
		result = false;
	}

	return result;
}
